package com.ym.pms.controller;

import com.github.pagehelper.PageInfo;
import com.ym.pms.model.Building;
import com.ym.pms.model.Suite;
import com.ym.pms.service.SuiteService;
import com.ym.pms.utils.JSONResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "套房主表接口")
@RequestMapping("/suites")
public class SuiteController {
    @Autowired
    private SuiteService service;



    @PreAuthorize("hasAnyRole('ADMIN','PROPERTY')")
    @ApiOperation(value = "添加套房主表")
    @PostMapping
    public JSONResult add(Suite record){
        int result = service.add(record);
        if (result >= 1) {
            return JSONResult.ok("添加成功");
        } else {
            return JSONResult.errorMsg("添加失败");
        }
    }

    @PreAuthorize("hasAnyRole('ADMIN','PROPERTY')")
    @ApiOperation(value = "删除套房",notes = "根据套房编码删除主表对应的所有内容")
    @DeleteMapping("/delete")
    @ApiImplicitParam(name = "suCoding", value = "套房编码", required = true, dataType = "String" )
    public JSONResult delete(@RequestParam("suCoding")String suCoding){
        int result = service.delete(suCoding);
        if (result >= 1) {
            return JSONResult.ok("删除成功");
        } else if(result == -1){
            return JSONResult.errorMsg("套房编码为空");
        } else if(result == -2){
            return JSONResult.errorMsg("未找到这条数据");
        }else{
            return JSONResult.errorMsg("删除失败");
        }
    }

    @PreAuthorize("hasAnyRole('ADMIN','PROPERTY')")
    @ApiOperation(value = "更新套房主表")
    @PostMapping("/update")
    public JSONResult update(Suite record){
        int result = service.update(record);
        if (result >= 1) {
            return JSONResult.ok("更新成功");
        } else if(result == -1){
            return JSONResult.errorMsg("套房编码为空");
        } else if(result == -2){
            return JSONResult.errorMsg("未找到这条数据");
        }else{
            return JSONResult.errorMsg("更新失败");
        }
    }


    @PreAuthorize("hasAnyRole('USER','ADMIN','ARCHIVIST','PROPERTY','BASIC')")
    @ApiOperation(value = "分页查询套房主表",notes = "分页查询出套房主表所有内容")
    @GetMapping("/findAll")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true, dataType = "int" ),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int" )
    })
    public JSONResult findAll(@RequestParam("pageNum")int pageNum, @RequestParam("pageSize")int pageSize){
        PageInfo all = service.findAll(pageNum, pageSize);
        return JSONResult.ok(all);
    }

    @PreAuthorize("hasAnyRole('USER','ADMIN','ARCHIVIST','PROPERTY','BASIC')")
    @ApiOperation(value = "分页模糊查询套房表信息")
    @GetMapping("/fuzzyQuery")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true, dataType = "int" ),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int" )
    })
    public JSONResult fuzzyQuery(Suite suite, @RequestParam("pageNum")int pageNum, @RequestParam("pageSize")int pageSize){
        PageInfo pageInfo = service.fuzzyQuery(suite, pageNum, pageSize);
        return JSONResult.ok(pageInfo);
    }

    @PreAuthorize("hasAnyRole('USER','ADMIN','ARCHIVIST','PROPERTY','BASIC')")
    @ApiOperation(value = "根据coding查询套房")
    @GetMapping("/findByCoding")
    @ApiImplicitParam(name = "su_Coding", value = "套房编号", required = true, dataType = "String" )
    public JSONResult findByCoding(@RequestParam("su_Coding")String su_Coding){
        Suite suite = service.findByCoding(su_Coding);
        return JSONResult.ok(suite);
    }

    @PreAuthorize("hasAnyRole('USER','ADMIN','ARCHIVIST','PROPERTY','BASIC')")
    @ApiOperation(value = "分页查询所有已启用的套房")
    @GetMapping("/findByStartUsing")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true, dataType = "int" ),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int" )
    })
    public JSONResult findByStartUsing(@RequestParam("pageNum")int pageNum, @RequestParam("pageSize")int pageSize){
        PageInfo pageInfo = service.findByStartUsing(pageNum, pageSize);
        return JSONResult.ok(pageInfo);
    }

    @PreAuthorize("hasAnyRole('USER','ADMIN','ARCHIVIST','PROPERTY','BASIC')")
    @ApiOperation(value = "分页查询所有已启用并且还有空铺位的套房")
    @GetMapping("/findByStartUsingAndVacant")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true, dataType = "int" ),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int" )
    })
    public JSONResult findByStartUsingAndVacant(@RequestParam("pageNum")int pageNum, @RequestParam("pageSize")int pageSize){
        PageInfo pageInfo = service.findByStartUsingAndVacant(pageNum, pageSize);
        return JSONResult.ok(pageInfo);
    }
}
